package com.codeoss.module.sale.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.DateUtil;
import com.codeoss.module.ir.service.IIrDocTypeService;
import com.codeoss.module.ir.service.IIrSequenceService;
import com.codeoss.module.sale.dao.SaleReturnMapper;
import com.codeoss.module.sale.model.SaleOrderLine;
import com.codeoss.module.sale.model.SaleReturn;
import com.codeoss.module.sale.model.SaleReturnLine;
import com.codeoss.module.sale.service.ISaleOrderLineService;
import com.codeoss.module.sale.service.ISaleReturnLineService;
import com.codeoss.module.sale.service.ISaleReturnService;
import com.codeoss.module.stock.model.StockOut;
import com.codeoss.module.stock.model.StockOutLine;
import com.codeoss.module.stock.service.IStockOutLineService;
import com.codeoss.module.stock.service.IStockOutService;

@Service
public class SaleReturnService extends BaseService<SaleReturnMapper, SaleReturn> implements ISaleReturnService,ApplicationContextAware{
    private ApplicationContext applicationContext;
	@Autowired
	private IIrDocTypeService irDocTypeService;
	@Autowired
	private DateUtil dateUtil;
	@Autowired
	private IIrSequenceService irSequenceService;
	@Autowired
	private ISaleReturnLineService saleReturnLineService;
	@Override
	@Transactional
	public SaleReturn generateSaleReturn(Long stockOutId) {
		// TODO Auto-generated method stub
		IStockOutService stockOutService=applicationContext.getBean(IStockOutService.class);
		IStockOutLineService stockOutLineService=applicationContext.getBean(IStockOutLineService.class);
		StockOut stockOut=stockOutService.getById(stockOutId);
		String docStatus=stockOut.getDocStatus();
		if(!"CO".equals(docStatus)) {
			throw new RuntimeException("请先完成单据");
		}
		
		SaleReturn saleReturn=new SaleReturn();
		Long docTypeId=irDocTypeService.getIrDocTypeIdByCode("salereturn");
		saleReturn.setIrDocTypeId(docTypeId);
		saleReturn.setResPartnerId(stockOut.getResPartnerId());
		saleReturn.setStockOutId(stockOutId);
		saleReturn.setDocStatus("DR");
		saleReturn.setDateReturn(dateUtil.getSqlNowDatetime());
		saleReturn.setDocumentNo(irSequenceService.getNextSequenceNo("salereturn"));
		insert(saleReturn);
		
		List<StockOutLine> stockOutLineList=stockOutLineService.getLineListByStockOutId(stockOutId);
		ISaleOrderLineService saleOrderLineService=applicationContext.getBean(ISaleOrderLineService.class);
		for(StockOutLine stockOutLine:stockOutLineList) {
			SaleReturnLine saleReturnLine=new SaleReturnLine();
			saleReturnLine.setStockOutLineId(stockOutLine.getId());
			saleReturnLine.setProductProductId(stockOutLine.getProductProductId());
			saleReturnLine.setProductAttrSetInstanceId(stockOutLine.getProductAttrSetInstanceId());
			saleReturnLine.setUomUomId(stockOutLine.getUomUomId());
			saleReturnLine.setQtyEntered(stockOutLine.getQtyEntered());
			Long saleOrderLineId=stockOutLine.getSaleOrderLineId();
			SaleOrderLine saleOrderLine=saleOrderLineService.getById(saleOrderLineId);
			saleReturnLine.setPriceEntered(saleOrderLine.getPriceEntered());
			saleReturnLine.setAccountTaxId(saleOrderLine.getAccountTaxId());
			saleReturnLine.setQtyReturn(saleOrderLine.getQtyOrdered());
			saleReturnLine.setAmountSubtotal(saleOrderLine.getAmountSubtotal());
			saleReturnLine.setDiscount(saleOrderLine.getDiscount());
			saleReturnLine.setSaleReturnId(saleReturn.getId());
			saleReturnLineService.insert(saleReturnLine);
		}
		return saleReturn;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<SaleReturnLine> lineList=saleReturnLineService.getLineListBySaleReturnId(id);
		if(lineList==null ||lineList.size()==0 ) {
			throw new RuntimeException("没有行明细");
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		SaleReturn saleReturn=getById(id);
		saleReturn.setApproved(true);
		update(saleReturn);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		SaleReturn saleReturn=getById(id);
		saleReturn.setApproved(false);
		update(saleReturn);
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		SaleReturn saleReturn=getById(id);
		saleReturn.setDocStatus("CO");
		update(saleReturn);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		SaleReturn saleReturn=getById(id);
		saleReturn.setDocStatus(docStatus);
		update(saleReturn);
	}
}